package jaoso.framework.service;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

/**
 * @author Edgeloner edgeloner@yahoo.com.cn
 */
public final class ServiceLocator
{
    private static ServiceLocator instance;

    static
    {
        instance = new ServiceLocator();
    }

    private ApplicationContext context;

    /**
     * Create a new ServiceLocator Instance.
     */
    private ServiceLocator()
    {
    }

    /**
     * @return ServiceLocator Instance
     */
    public static ServiceLocator getInstance()
    {
        return instance;
    }

    /**
     * get manager service
     * 
     * @param service
     *            service name
     * 
     * @return manager service
     */
    public Object getService(final String service)
    {
        return context.getBean(service);
    }

    private ApplicationContext getContext()
    {
        return context;
    }

    public void setContext(ApplicationContext context)
    {
        this.context = context;
    }

    public void autowireService(Object bean)
    {
        ((AbstractApplicationContext) context).getBeanFactory()
                .autowireBeanProperties(bean,
                        AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    }
}